﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;

namespace BuildHelper.Setup.Pliki
{
    public partial class FormPlikiRecord : MyBaseSqlCeForm
    {
        public FormPlikiRecord()
        {
            InitializeComponent();
        }
        public FormPlikiRecord(int id)
        {
            InitializeComponent();
            CurrentRecord = id;
        }
        private void rep_STP_PL_PLIK_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (modeProvider1.CurrentMode != ProgNetComponentsDx.Enums.FormMode.Read)
            {
                GridView gv = gcMain.FocusedView as GridView;
                if (gv != null)
                {
                    int parentRow = gv.SourceRowHandle;
                    DataRow group = gvMain.GetDataRow(parentRow);
                    if (group != null)
                    {
                        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            foreach (string str in openFileDialog1.FileNames)
                            {
                                AddFile(group, str);
                            }
                        }
                    }
                }
            }
        }
        
        protected override bool GetRecord()
        {
            bool res = base.GetRecord();
            if (res)
                plikiDataSet1.STP_PLIKI.STP_PL_GRUPAColumn.DefaultValue = MainRow["STP_GRPL_ID"];
            return res;
        }
        protected override void CopyRecord()
        {
            base.CopyRecord();
            this.sTP_GRPL_NAZWATextEdit.EditValue = DBNull.Value;
        }
        private void AddFile(DataRow group, string str)
        {
            string file = GetFormatedFileName(str.Trim());
            DataRow plik;
            if (!this.plikiDataSet1.STP_PLIKI.RowExists("STP_PL_GRUPA = " + group["STP_GRPL_ID"] + " AND STP_PL_PLIK = " + file.EscapeSql()))
            {
                plik = this.plikiDataSet1.STP_PLIKI.NewRow();
                plik["STP_PL_PLIK"] = file;
                plik["STP_PL_GRUPA"] = group["STP_GRPL_ID"];
                plikiDataSet1.STP_PLIKI.Rows.Add(plik);
            }
        }
        private string GetFormatedFileName(string file)
        {
            string s = file.XReplace("/", "\\");
            if (file.XMatch("\\\\apps\\\\"))
            {
                s = "%ROOT%\\apps\\" + file.XReplace(".*?\\\\apps\\\\", "");
            }
            return s;
        }

        private void gvMain_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            GridView gv = sender as GridView;
            if (gv != null)
            {
                string plik = e.Value.AsString();
                string nplik = GetFormatedFileName(plik);
                if (nplik != plik)
                    gv.SetRowCellValue(e.RowHandle, e.Column, nplik);
            }
        }

        private void bbiDodajZeSchowka_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRow group = MainRow;
            if (group != null)
            {
                string str = Clipboard.GetText();
                string[] parts = null;
                if (str.IsNullOrEmpty())
                {
                    System.Collections.Specialized.StringCollection col = Clipboard.GetFileDropList();
                    if (col != null && col.Count > 0)
                    {
                        parts = new string[col.Count];
                        col.CopyTo(parts, 0);
                    }
                }
                else
                {
                    parts = str.Split(';', '\n');
                }
                if (parts != null && parts.Length > 0)
                {
                    foreach (string s in parts)
                    {
                        if (s.IsNotNullOrEmpty())
                            AddFile(group, s);
                    }
                }
                else
                {
                    this.ShowDxInfoDialog("Brak plików w schowku.");
                }
            }
        }
   
    }
}
